package com.situ.company.employee.service.impl;

import java.util.List;
import com.situ.company.employee.dao.IEmployeeDao;
import com.situ.company.employee.dao.impl.EmployeeDaoImpl;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.IEmployeeService;
import com.situ.company.util.FmtUpload;
import com.situ.company.util.MD5;
   
public class EmployeeServiceImpl implements IEmployeeService {

	private IEmployeeDao dao = new EmployeeDaoImpl();
	private static String passDefault = "123456";
	
	@Override 
	public String insert(EmployeeModel model) {
		EmployeeModel m1 = new EmployeeModel(model.getCode());
		EmployeeModel mdb = dao.selectModel(m1);
		if(mdb!=null)
			return "repeat";
		String pass = model.getPass();
		if(pass==null||pass.isEmpty())
			pass = passDefault;
		model.setPass(MD5.encode(pass));
		return dao.insert(model)+"";
	}

	@Override
	public String delete(EmployeeModel model) {
		
		return dao.deleteByCode(model)+"";
	}

	@Override
	public String update(EmployeeModel model) {
		
		return dao.updateAll(model)+"";
	}

	@Override
	public List<EmployeeModel> selectList(EmployeeModel model) {
		String code = model.getCode();
		model.setCode(code==null? "%":"%"+code+"%");
		String name = model.getName();
		model.setName(name==null? "%":"%"+name+"%");
		return dao.selectList(model);
	}

	@Override
	public EmployeeModel selectModel(EmployeeModel model) {
		return dao.selectModel(new EmployeeModel(model.getCode()));
	}

	@Override
	public String login(EmployeeModel model) {
		EmployeeModel mdb =  selectModel(model);
		if(mdb==null)
			return "0";
		return mdb.getPass().equals(model.getPass())?"1":"2";
	}
	
	public Integer selectCount(EmployeeModel model) {
		EmployeeModel m1 = new EmployeeModel();
		String code = model.getCode();
		m1.setCode(code==null? "%":"%"+code+"%");
		String name = model.getName();
		m1.setName(name==null? "%":"%"+name+"%");
		return dao.selectCount(m1);
		
	}

	public String setDept(EmployeeModel model) {
		return dao.updateActive(model)+"";
	}
	
	
	@Override
	public String updPass(EmployeeModel model) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String resetPass(EmployeeModel model) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String updateActive(EmployeeModel model) {
		
		return dao.updateActive(model)+"";
	}

	@Override
	public String delPic(EmployeeModel model) {
		EmployeeModel mdb = selectModel(model);
		String image = mdb.getImage();
		FmtUpload.delFile(image);
		model.setImage("");
		return dao.updateActive(model)+"";
	}
 
}
